

#pragma once

#include "cocos2d.h"
#include "cocos-ext.h"
USING_NS_CC;
USING_NS_CC_EXT;

#include "formation.h"
#include "unit.h"

enum class FormationSceneTouchPlace {
	blank,
	formation_view,
	equipment_detail_view,
	soldier,
	dragging_soldier,
	unit_bar,
	unit,
	create_unit,
	addUnit,
	slide_formation_view,
	show_unit_bar,
	hide_unit_bar,
};

namespace cocos2d {
	class CCSkeletalSprite;
}

class Unit;
class UnitDetailLayer;

class FormationScene : public CCScene, public CCTargetedTouchDelegate, public CCKeypadDelegate {
public:
	FormationScene();
	virtual ~FormationScene();

	static FormationScene* create(int index);

public:
	FormationInfo formationInfo;
	int formationIndex;
private:
	virtual bool init(int index);
	void update(float dt);
	void handleMessage();

	void createFormationUIThings();
	void createLeftBarUIThings();
	void createUnitBarUIThings();
	void updateUnitBarItems();

	void onSave(CCObject* sender);
	void onReset(CCObject* sender);
	void onBack(CCObject* sender);

	void createFormation();

	GridInfo getGridIndexAtPoint(const CCPoint& pt);
	Unit* getSoldierByID(int id);
	float getUnitScaleForUnitBar(Unit* unit);

	void setSoldierPosition(Unit* unit);
	void updateUnitOccupiedGrids(Unit* unit, int line, int row);
	void updateGridsState(Unit *unit, int line, int row);
	void clearGridsState();
	bool canPlaceUnitHere(Unit *unit, int line, int row);
	void removeExistedSoldier(int id);
	void removeTargetGridsSoldier(Unit* unit, int line, int row);

	void showUnitBar();
	void hideUnitBar();
	void createUnit(CCObject* sender);
	void editUnit(CCObject* sender);
	void deleteUnit(CCObject* sender);
	void showUnitDetail();

	virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
	virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);
	virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);
	virtual void ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent);

	void handleDroppingSoldier(CCPoint const &pt);
	void handleAddingUnit(CCPoint pt);
	void changeSquadOrderState(CCPoint pt);
	void slideFormationView(float dx);

	virtual void keyBackClicked();
	virtual void keyMenuClicked();

private:
	CCLayer* _layer;
	CCLayer* _layerFormation;
	UnitDetailLayer* _unitDetailLayer;

	CCPoint _formationViewInitialPosition;

	Unit* _soldiers[TOTAL_LINES][ROWS_PER_LINE];
	int _gridsState[TOTAL_LINES][ROWS_PER_LINE];
	CCSprite* _gridsStateIndicator[TOTAL_LINES][ROWS_PER_LINE];

	CCNode* _unitBar;
	CCSprite* _sprCreateUnit;

	vector<Unit*> _arrUnitBarItems;

	int _selectedUnitBarItemIndex;
	bool _isShownUnitBar;

	CCSprite* _leftBar;
	CCSprite* _garbageZone;

	CCMenuItemSprite* _btnSave;
	CCMenuItemSprite* _btnReset;
	CCMenuItemSprite* _btnExit;
	CCMenuItemSprite* _btnUnitBarSwitch;

	CCSprite* _squadsBody[SQUAD_COUNT];
	CCLabelTTF* _squadsFooter[SQUAD_COUNT];
	CCSprite* _squadsHeader[SQUAD_COUNT];

	Unit* _draggingSoldier;
	Unit* _addingUnit;

	FormationSceneTouchPlace _touchPlace;
	GridInfo _draggingStartGrid;
	GridInfo _draggingTargetGrid;

	virtual void onEnter();
	virtual void onExit();

	char buf[300];
};
